<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Goodstype;
use Illuminate\Support\Facades\Redis;
use DB;

class AdminGoodsTypeListController extends Controller
{

	/**
	 * 显示分类列表
	 * @param Request $req [description]
	 */
    public function AdminGoodsTypeList(Request $req)
    {
    	//接收到搜索的数据
    	$sname = $req->input('sname');

    	if (empty($sname)) {
    		$where = [];
    	} else {
    		$where = [['type_name', 'like', "%$sname%"]];
    	}

    	$type =  DB::select('select id,type_name,pid,path from shop_goods_types order by concat(path, id)');
    	return view('Admin.goods.product-typelist', ['type' => $type]);
    }


    /**
     * 显示添加分类
     * @param string $id [description]
     */
    public function AdminGoodsTypeAdd(Request $req)
    {
        $id = $req->id;

    	if(!empty($id)) {

    		$type = Goodstype::select()->where('id', $id)->first();

    		// dd($type);

    		return view('Admin.goods.product-typeadd', ['type' => $type]);

    	}

    	return view('Admin.goods.product-typeadd');
    }

    /**
     * 处理添加分类
     * @param Request $req [description]
     */
    public function AdminGoodsAddType(Request $req)
    {
    	$name = $req->input('name');
    	$pid = $req->input('pid');
        $created_at = date('Y:m:d H:i:s');
        $updated_at = date('Y:m:d H:i:s');
    	// dd($pid);
    	//判断分类名不能为空
    	if (empty($name)) {

    		return back()->with('msg', '分类名不能为空');
    	}

    	//判断分类名是否存在
    	$lol = Goodstype::select()->where([['type_name', $name], ['pid', $pid]])->first();

    	if ($lol) {
    		return back()->with('msg', '分类名已存在');
    	}

    	// $type = Goodstype::select()->where('id', $pid)->first();
    	// dd($pid);
    	// 判断是否有父类
    	if ($pid == '0') {

    		$path = $pid.',';

    	} else {

    		$path = '0,'.$pid.',';
    	}
    	// var_dump($path);
    	$res = Goodstype::insertGetId([
    		'type_name' => $name,
    		'pid' => $pid,
    		'path' => $path,
            'created_at' => $created_at,
            'updated_at' => $updated_at
    	]);

        $hashKey = 'goods_types_hash_data:';

        //把一个数据存到Redis哈希键
        Redis::hMSet($hashKey.$res, ['id' => $res, 'type_name' => $name]);

        if ($pid == '0') {

            $setKey = 'goods_types_set_data';

        } else {

            $setKey = 'goods_types_set_data_sons:'.$pid;
        }

        //把哈希键存到集合中
        Redis::sAdd($setKey, $hashKey.$res);

    	if (!$res) {

    		return back()->with('msg', '添加失败');
    	}

    	return back()->with('msg', '添加成功');

    }



    /**
     * 删除分类
     */
    public function AdminGoodsDelType(Request $req)
    {
    	$id = $req->id;

    	// $type = Goodstype::where('id', $req->id)->first();
    	//根据id查询是否有子分类
    	$res = Goodstype::where('pid', $id)->first();

    	if (!empty($res)) {
    		return [
            'msg' => '请先删除子分类',
            'status' => 403,
          ];
    	}

    	//根据id查询分类下是否有商品
    	// $goods = DB::table('shop_goods')->where('type_id', $req->id)->first();
    	$resu = Goodstype::find($id);
    	$goods = $resu->goods;


    	if (!empty($goods)) {
    		return [
            'msg' => '请先删除分类下的商品',
            'status' => 404,
          ];
    	}

        //判断是父类还是子类
        $type = Goodstype::where('id', $id)->first();

        if ($type->pid == 0) {
            //是父类
            $setKey = 'goods_types_set_data';

        }else {
            //是子类
            $setKey = 'goods_types_set_data_sons:'.$type->pid;
        }

        $hashKey = 'goods_types_hash_data:'.$id;
        //删除集合中的哈希的键
        Redis::sRem($setKey, $hashKey);
        //删除哈希的键
        Redis::del($hashKey);

    	$lol = Goodstype::where('id', $id)->delete();




        if (!$lol) {

           return [
            'msg' => '删除失败',
            'status' => 404,
            ];
        }

    	return [
            'msg' => '已删除',
            'status' => 200,
        ];
    }

    /**
     * 显示编辑分类
     */
    public function AdminGoodsEditType(Request $request)
    {
    	// dd($request->id);
    	$type = Goodstype::select()->where('id', $request->id)->first();

    	return view('Admin.goods.product-typeedit', ['type' => $type]);
    }


    /**
     * 处理编辑分类
     */
    public function AdminGoodsTypeEdit(Request $request)
    {
    	$id = $request->input('id');
    	$name = $request->input('name');
        $updated_at = date('Y:m:d H:i:s');
    	//判断分类名不能为空
    	if (empty($name)) {

    		return back()->with('msg', '分类名不能为空');
    	}

    	$lol = DB::table('shop_goods_types')->where('id', $id)->update(['type_name' => $name, 'updated_at' => $updated_at]);
        if (!$lol) {

            return back()->with('msg', '修改失败');

        }

         //判断是父类还是子类
        $type = Goodstype::where('id', $id)->first();

        if ($type->pid == 0) {
            //是父类
            $setKey = 'goods_types_set_data';

        }else {
            //是子类
            $setKey = 'goods_types_set_data_sons:'.$type->pid;

        }

        $hashKey = 'goods_types_hash_data:'.$id;
        //删除集合中哈希的键
        Redis::sRem($setKey, $hashKey);
        //删除哈希的键
        Redis::del($hashKey);
        //把哈希的键添加到集合中
        Redis::sAdd($setKey, $hashKey);
        //把数据存到哈希的键
        Redis::hMSet($hashKey, ['id' => $id, 'type_name' => $name]);


    	return back()->with('msg', '修改成功');
    }
}
